SOLIDWORKS-API
对象拓扑图:
技能树
获得程序
(ISldWorks)
1 2 3 4 5 6
| public static SldWorks swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
如果是有多个版本的SW,可能需要区分: (SldWorks)Marshal.GetActiveObject("SldWorks.Application.26"); (SldWorks)Marshal.GetActiveObject("SldWorks.Application.27"); (SldWorks)Marshal.GetActiveObject("SldWorks.Application.28");
|
获得文件
1、打开方式
1
| swApp.OpenDoc(filename, (int)swDocumentTypes_e.swDocPART);
|
2、激活当前文件:
1 2 3
| PartDoc swPart = (AssemblyDoc)swApp.ActiveDoc; AssemblyDoc swAssy = (AssemblyDoc)swApp.ActiveDoc; DrawingDoc swDraw = (AssemblyDoc)swApp.ActiveDoc;
|
获得属性
(CustomPropertyManager)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| CustomPropertyManager cusPropMgr = swDoc.Extension.CustomPropertyManager[""];
Get:
cusPropMgr.GetAll2(ref vPropNamesObject, ref vPropTypes, ref vPropValues, swCustomInfoGetResult_e.swCustomInfoGetResult_NotPresent); object[] vPropNames = (object[])vPropNamesObject; if (vPropNames == null) { return; }
Set: 增:Add(属性名,类型,属性值,添加时的设置) cusPropMgr.Add3(PropertyName, (int)swCustomInfoType_e.swCustomInfoText, PropertyValue, (int)swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd); 改: vPropNames[i]=”” 删: cusPropMgr.Delete2((string)vPropNames[i]);
|
获得特征
遍历特征对象,根据特征名称找到特征对象来操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
public static void TraverseFeatures(bool isTopLevel) { Feature thisFeat = (Feature)swDoc.FirstFeature(); Feature curFeat = default(Feature); curFeat = thisFeat;
while ((curFeat != null)) { Debug.Print(curFeat.Name); bool isShowDimension = false; if (isShowDimension == true) ShowDimensionForFeature(curFeat);
Feature nextFeat = default(Feature); if (isTopLevel) { nextFeat = (Feature)curFeat.GetNextFeature(); } else { nextFeat = null; } curFeat = nextFeat; nextFeat = null; } }
|
获得零部件
dynamic IAssemblyDoc.GetComponents(bool ToplevelOnly);
通常用object[]接受,可在实际使用时转其他可用类型,常见转string[]。
举例:
1 2 3 4 5 6
| public static string[] GetComps() { AssemblyDoc swAssy = (AssemblyDoc)swApp.ActiveDoc; string[] Comppaths = (string[])swAssy.GetComponents(true); return Comppaths; }
|
获得图纸
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static void GetSheetNames() { Sheet drwSheet = (Sheet)swDraw.GetCurrentSheet(); object[] sheetNames = (object[])swDraw.GetSheetNames(); object[] views = (object[])drwSheet.GetViews();
foreach (View view in views) { DrawingComponent comp = view.RootDrawingComponent; Debug.Print(comp.Name); object[] childrencomps = (object[])comp.GetChildren(); for (int i = childrencomps.GetLowerBound(0); i <= childrencomps.GetUpperBound(0); i++) { Debug.Print("零部件是" + ((DrawingComponent)childrencomps[i]).Name); } } }
|
常用操作
多选尺寸修改操作:
宏程序
尺寸标注
Sw_实体转换钣金
solidwork程序在零件类型里有个“钣金”类型。这个钣金零件会有一些特殊的属性。如果是通过其他格式(如stp,x_t等)打开的模型,软件默认只会识别成一个实体特征。实体特征将不会带有钣金的属性。
使用我去找了相关程序来实现:实体特征转钣金的方法。